package com.bocai.test;

import java.util.*;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

public class RSASign {

	public static final String  SIGN_ALGORITHMS = "SHA1WithRSA";
	public static void main(String[] args) {
//企业ID：FM  应用ID:FMBALL     api：https://dev.tcmapi.cn
		System.out.println("************************TCM-API SIGNATURE WITH SHA1***************************");
		//公钥
		String publicKey ="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCzJAQ/zaRYzh5K8j0VQVVLVtaA" +
				"WvPzI1m7kUYH8Ix+50ZSGBsuES4rERQKadWSIBi02WmaeHpu+c0Xc+wPlEHbkuli" +
				"h3T/+3mcA83Ht+qidj1xlHHSZW4TJge9Bnhfc5j7srhcTsCXLbJFNFVk8kyzTt2D" +
				"h1O4f5FNGxPOCTqnZwIDAQAB";
		//私钥
		String privateKey1 = "MIICWwIBAAKBgQCzJAQ/zaRYzh5K8j0VQVVLVtaAWvPzI1m7kUYH8Ix+50ZSGBsu" +
				"ES4rERQKadWSIBi02WmaeHpu+c0Xc+wPlEHbkulih3T/+3mcA83Ht+qidj1xlHHS" +
				"ZW4TJge9Bnhfc5j7srhcTsCXLbJFNFVk8kyzTt2Dh1O4f5FNGxPOCTqnZwIDAQAB" +
				"AoGARnF0Rk6sQMuUt+33vSL60mxsXv7lDQgqogpnWnnqFYEGcwPNtFjHKeJGWi3x" +
				"8Ms0S/FuiHK12mvajU1lV176Yh93dcDLXepGdlT0H92SDs6RoERFNcoT/h498NiI" +
				"yM6/UGVvTMQw3cGPK5Wsy0E9nwUwsudGRJcCzqPUa0Sg56kCQQDr1ZLiKW57gkjl" +
				"hRIx8crTO2df72sWbh9a4OTszxWHh9PDf/qFyMbu8QNaDC02r1rej58CEG9HgTnh" +
				"fq7b8GNTAkEAwnVqvLewZaXsH0NOlX/EbcTuAShavVrX3iTvnk8WfYi8XZDyyBZr" +
				"Z4sOziImPUedkZ1Uwtb9oZBpTbeo/GUdHQJAJI+HpegmaJ9aDbsDhxqlBvo+nHDR" +
				"eTi5Q615z/yKqeg0vCoq/HKT5JcYne0lVUT6IWbCj99cYQl3n620DgTGYQJAVQek" +
				"yaqEvp3j9vOmfCanTNxsuiLcMqpzdBawlIe+rNvKihtknet3wYoDyk0SbX9QPikO" +
				"tDfwJeKQuUQS53KLXQJAUpNJnSxdEcK1jh1b9Qxq1ACwfyGeaKarbJbirXRuEmrO" +
				"qCx/RcUk5GT3PHFS51H7p6IY0q2SCRYDarYXI+svoQ==";
		String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALMkBD/NpFjOHkry" +
				"PRVBVUtW1oBa8/MjWbuRRgfwjH7nRlIYGy4RLisRFApp1ZIgGLTZaZp4em75zRdz" +
				"7A+UQduS6WKHdP/7eZwDzce36qJ2PXGUcdJlbhMmB70GeF9zmPuyuFxOwJctskU0" +
				"VWTyTLNO3YOHU7h/kU0bE84JOqdnAgMBAAECgYBGcXRGTqxAy5S37fe9IvrSbGxe" +
				"/uUNCCqiCmdaeeoVgQZzA820WMcp4kZaLfHwyzRL8W6IcrXaa9qNTWVXXvpiH3d1" +
				"wMtd6kZ2VPQf3ZIOzpGgREU1yhP+Hj3w2IjIzr9QZW9MxDDdwY8rlazLQT2fBTCy" +
				"50ZElwLOo9RrRKDnqQJBAOvVkuIpbnuCSOWFEjHxytM7Z1/vaxZuH1rg5OzPFYeH" +
				"08N/+oXIxu7xA1oMLTavWt6PnwIQb0eBOeF+rtvwY1MCQQDCdWq8t7BlpewfQ06V" +
				"f8RtxO4BKFq9WtfeJO+eTxZ9iLxdkPLIFmtniw7OIiY9R52RnVTC1v2hkGlNt6j8" +
				"ZR0dAkAkj4el6CZon1oNuwOHGqUG+j6ccNF5OLlDrXnP/Iqp6DS8Kir8cpPklxid" +
				"7SVVRPohZsKP31xhCXefrbQOBMZhAkBVB6TJqoS+neP286Z8JqdM3Gy6ItwyqnN0" +
				"FrCUh76s28qKG2Sd63fBigPKTRJtf1A+KQ60N/Al4pC5RBLncotdAkBSk0mdLF0R" +
				"wrWOHVv1DGrUALB/IZ5opqtsluKtdG4Sas6oLH9FxSTkZPc8cVLnUfunohjSrZIJ" +
				"FgNqthcj6y+h";
	    String sign = sign("YoungMan",privateKey,"utf-8");
		System.out.println(sign);
		
		
		boolean flag = verify("YoungMan",sign,publicKey,"utf-8");
		
		System.out.println(flag);
		System.out.println("*******************************************************");

	}
	
	 /**
	   * RSAǩ��
	   * @param content ��ǩ������
	   * @param privateKey �̻�˽Կ
	   * @param input_charset �����ʽ
	   * @return ǩ��ֵ
	   */
	   public static String sign(String content, String privateKey, String input_charset)
	   {
	        try 
	        {
	           byte[] decode =Base64.getDecoder().decode(privateKey);
	        	
	           PKCS8EncodedKeySpec priPKCS8   = new PKCS8EncodedKeySpec(decode);
	           KeyFactory keyf= KeyFactory.getInstance("RSA");
	           PrivateKey priKey= keyf.generatePrivate(priPKCS8);
				//����4
	            java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS);

	            signature.initSign(priKey);
				//����3
	            signature.update(content.getBytes(input_charset) );

				//����5
	            byte[] signed = signature.sign();
	            
	            return Base64.getEncoder().encodeToString(signed);
	        }
	        catch (Exception e) 
	        {
	           e.printStackTrace();
	        }
	        
	        return null;
	    }
	   /**
	    * RSA��ǩ�����
	    * @param content ��ǩ������
	    * @param sign ǩ��ֵ
	    * @param public_key ��Կ
	    * @param input_charset �����ʽ
	    * @return ����ֵ
	    */
	    public static boolean verify(String content, String sign, String public_key, String input_charset)
	    {
	       try 
	       {
	          KeyFactory keyFactory = KeyFactory.getInstance("RSA");
	            byte[] encodedKey = Base64.getDecoder().decode(public_key);
	            PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
	          java.security.Signature signature = java.security.Signature
	          .getInstance(SIGN_ALGORITHMS);
	       
	          signature.initVerify(pubKey);
	          signature.update( content.getBytes(input_charset) );
	       
	          boolean bverify = signature.verify( Base64.getDecoder().decode(sign) );
	          return bverify;
	          
	       } 
	       catch (Exception e) 
	       {
	          e.printStackTrace();
	       }
	       
	       return false;
	    }
}
